package com.jjrwa.from;

import com.jjrwa.component.BaseFrame;
import com.jjrwa.component.ITableDataEntity;
import com.jjrwa.component.TableDataHandle;
import com.jjrwa.transform.ICreateUI;
import lombok.Getter;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

@Getter
public class UIFieldTable<T extends ITableDataEntity> extends BaseFrame {
    private JPanel panel1;
    private JButton cancelButton;
    private JButton OKButton;
    private JTable table1;

    private TableDataHandle<T> handle;
    private ICreateUI createUI;

    public UIFieldTable(TableDataHandle<T> handle, ICreateUI createUI) {
        this(handle, new Dimension(600, 300), "字段详情", createUI);
    }

    public UIFieldTable(TableDataHandle<T> handle, Dimension size, String title, ICreateUI createUI) {
        super();
        this.handle = handle;
        this.createUI = createUI;

        JFrame jFrame = this.windowCenter(title, this.panel1, size);
        jFrame.getRootPane().setDefaultButton(OKButton);

        this.cancelButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                jFrame.dispose();
            }
        });

        this.OKButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                handle.updateValues(table1);
                jFrame.dispose();
            }
        });
    }

    private void createUIComponents() {
        this.table1 = createUI.createTable(this.handle);
    }
}
